package com.fr.report.io;

import com.fr.base.Style;
import com.fr.report.CellElement;
import com.fr.report.PageSet;
import com.fr.report.PaperSetting;
import com.fr.report.Report;
import com.fr.report.ReportPage;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.PackedReport;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportUtils;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.Format;
import java.util.Iterator;
import java.util.List;

public class TextExporter extends AbstractAppExporter
{
  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    if (paramResultWorkBook == null)
      throw new Exception("Report can not be null!");
    List localList = ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook);
    for (int i = 0; i < paramResultWorkBook.getReportCount(); ++i)
    {
      for (Object localObject = paramResultWorkBook.getReport(i); localObject instanceof PackedReport; localObject = ((PackedReport)localObject).getPackee());
      export(paramOutputStream, (Report)localObject, (PaperSetting)localList.get(i));
    }
  }

  private void export(OutputStream paramOutputStream, Report paramReport, PaperSetting paramPaperSetting)
    throws Exception
  {
    Object localObject;
    if (paramReport == null)
      throw new Exception("Report can not be null!");
    if ((paramReport instanceof LayerReport) || (paramReport instanceof LayerPageReport))
    {
      localObject = paramReport.generateReportPageSet(paramPaperSetting).getTraversingCase();
      export(paramOutputStream, (PageSet)localObject);
      ((PageSet)localObject).release();
    }
    else
    {
      localObject = new PrintWriter(new OutputStreamWriter(paramOutputStream));
      Iterator localIterator = paramReport.cellIterator();
      writeCells((PrintWriter)localObject, localIterator);
      ((PrintWriter)localObject).flush();
    }
  }

  private void writeCells(PrintWriter paramPrintWriter, Iterator paramIterator)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    String str = null;
    int i = 0;
    int j = 0;
    while (true)
    {
      int k;
      while (true)
      {
        if (!(paramIterator.hasNext()))
          break label184;
        CellElement localCellElement = (CellElement)paramIterator.next();
        Object localObject = localCellElement.getValue();
        if (localObject == null)
          localObject = "";
        Style localStyle = localCellElement.getStyle();
        Format localFormat = localStyle.getFormat();
        str = PaintUtils.valueToText(localObject, localFormat);
        k = localCellElement.getColumn();
        int l = localCellElement.getRow();
        if (l <= i)
          break;
        if (localStringBuffer != null)
          paramPrintWriter.println(localStringBuffer.toString());
        i = l;
        j = 0;
        localStringBuffer = new StringBuffer();
        localStringBuffer.append(str);
        j = k;
      }
      for (int i1 = j; i1 < k; ++i1)
        localStringBuffer.append("\t");
      localStringBuffer.append(str);
      j = k;
    }
    if (localStringBuffer != null)
      label184: paramPrintWriter.print(localStringBuffer.toString());
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    if (paramPageSet == null)
      throw new Exception("Error! pages is null");
    for (int i = 0; i < paramPageSet.size(); ++i)
    {
      ReportPage localReportPage = paramPageSet.getPage(i);
      if (localReportPage == null)
        return;
      PrintWriter localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream));
      if (localReportPage != null)
      {
        Iterator localIterator = localReportPage.cellIterator();
        writeCells(localPrintWriter, localIterator);
        localPrintWriter.println();
        localPrintWriter.println();
        localPrintWriter.println();
        localPrintWriter.println();
      }
      localPrintWriter.flush();
    }
  }
}